package com.example.wasaimovie.Controller;

import com.example.wasaimovie.Annotation.DisableBaseResponse;
import com.example.wasaimovie.Model.entity.UpLoad;
import com.example.wasaimovie.Model.support.ResponseResult;
import com.example.wasaimovie.Service.UpLoadService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.UUID;

@Controller
@RequestMapping("/api")
@Slf4j
public class ApiController {

    @Value("${movie.image.upload.path}")
    private String upload;

    @Value(("${movie.image.url.path}"))
    private String url;

    @Autowired
    UpLoadService service;

    /**
     * 文件上传
     * @param token 用户token
     * @param file 上传的文件
     */
    @PostMapping("/upload")
    @ResponseBody
    @CrossOrigin
    @DisableBaseResponse
    public ResponseResult<Object> postupLoad(@RequestParam(value = "token", required = false) String token,
                                             @RequestParam("file")MultipartFile file){
        log.info("ApiController --> postupload");
        File path = new File(upload);
        if (!path.exists()){
            // 如果文件夹不存在
            boolean mkdir = path.mkdir();
        }
        String originalFileName = file.getOriginalFilename();
        if (originalFileName != null){
            // 获取图片后缀
            int index = originalFileName.lastIndexOf(".");
            String voName = UUID.randomUUID().toString();
            String newName = voName + ".jpg";

            File up = new File(upload + newName);
            // 将图片转为base64
            UpLoad upLoad = new UpLoad();
//            byte[] data = null;
            try {
                file.transferTo(up);
//                InputStream is = new FileInputStream(up);
//                data = new byte[is.available()];
//                is.read(data);
//                is.close();
//                upLoad.setBytes(data);
//                upLoad.setId(newName);
//                service.save(upLoad);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return new ResponseResult<>("OK", url + voName);
        }
        return new ResponseResult<>(400, "图片上传失败");
    }

    /**
     * 获取图片信息
     * @param id 图片id
     * @param token 用户的token信息
     * @return 重定向到图片，返回信息
     */
    @GetMapping("/upload/{id}")
    @CrossOrigin
    public String getupload(@PathVariable String id,
                            String token){
        return "file/" + id + ".jpg";
    }
}
